package main

import (
	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/otel"
	"go.opentelemetry.io/otel/propagation"
	"log"
	srv "test.com/project-common"
	"test.com/project-project/config"
	"test.com/project-project/router"
	"test.com/project-project/tracing"
)

func main() {
	r := gin.Default()
	//集成jaeger
	tp, tpErr := tracing.JaegerTraceProvider()
	if tpErr != nil {
		log.Fatal(tpErr)
	}
	otel.SetTracerProvider(tp)
	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
	router.InitUserRpc()
	//grpc服务注册
	grpc := router.RegisterGrpc()
	//grpc服务注册到etcd
	router.RegisterEtcdServer()
	//初始化kafka生产者 并传出close方法   写入的进程一直打开  等待数据的写入
	c := config.InitKafkaWriter()
	//初始化kafka消费者
	reader := config.NewKafkaReader()
	//消费者一收到队列的数据  就将缓存里面的数据删除
	go reader.DeleteCache()
	stop := func() {
		//如果整个服务停了   grpc也就应该停止
		grpc.Stop()
		//停掉kafka
		c()
		reader.R.Close()
	}
	srv.Run(r, config.C.SC.Name, config.C.SC.Addr, stop)
}
